package com.qf.controller;

import com.qf.entity.Stu;
import com.qf.entity.StuVO;
import com.qf.service.StuService;
import com.qf.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * @author Qiao
 * @date 2025/8/15
 * @desc
 */
@RestController
@Api(tags = "单表查询")
public class StuController {
    @Autowired
    private StuService stuService;


    @GetMapping("/stu/{id}")
    @ApiOperation(value = "根据id查信息")
    // 根据id查信息
    public R findById(@PathVariable int id){
        Stu stu = stuService.findById(id);
        if (stu == null){
            R.fail(5000,"查无此人");
        }

        return R.success(stu);
    }

    // 查全部
    @GetMapping("/stu/findAll")
    @ApiOperation(value = "查所有学生信息")
    public  R findAll(){
        List<Stu> stu = stuService.findAll();
        return R.success(stu);
    }

    // 条件查询
    @GetMapping("/stu/search")
    @ApiOperation(value = "条件查询")
    public R search(@RequestParam Map<String,Integer> map){
        List<Stu> list = stuService.search(map);
        return R.success(list);
    }

    @PostMapping("/stu/add")
    @ApiOperation(value = "添加学生")
    public R add(@RequestBody Stu stu){
        stuService.add(stu);
        return R.success();
    }

    @PostMapping("/stu/edit/{id}")
    @ApiOperation(value = "更新")
    public R edit(@RequestBody Stu stu,@PathVariable int id){
        stu.setId(id);
        boolean isOk = stuService.edit(stu);
        if (!isOk){
            return R.fail(5000,"更新失败");
        }
        return R.success();
    }

    @GetMapping("/stu/delete/{id}")
    @ApiOperation(value = "根据id删除学生")
    // 根据id查信息
    public R deleteById(@PathVariable int id){
        boolean isOk = stuService.deleteById(id);
        if (!isOk){
            R.fail(5000,"查无此人");
        }

        return R.success();
    }

    @GetMapping("/stu/delete/batch")
    @ApiOperation(value = "批量删除")
    public R deleteBatch(@RequestParam List<Integer> ids){
        stuService.deleteBatch(ids);
        return R.success();
    }


    @GetMapping("/stuVO/{id}")
    @ApiOperation(value = "一对一关系查询")
    public R findStuVOById(@PathVariable int id){
        StuVO vo = stuService.findStuVOById(id);
        if (vo == null){
            R.fail(5000,"查无此人");
        }
        return R.success(vo);
    }

    @GetMapping("/stuVO")
    @ApiOperation(value = "一对一关系查询全部")
    public R findStuVOAll(){
        List<StuVO> voList = stuService.findStuVOAll();

        return R.success(voList);
    }


    @GetMapping("/subject/{id}")
    @ApiOperation(value = "一对多查询")
    public R findStudentAndAllSubjectByStuid(@PathVariable int id){
        List<StuVO> vo = stuService.findStudentAndAllSubjectByStuid(id);
        return R.success(vo);
    }

    @GetMapping("/teacher/{id}")
    @ApiOperation(value = "一对多查询")
    public R findStudentAndAllSubjectAndAllTeacherByStuid(@PathVariable int id){
        List<StuVO> vo = stuService.findStudentAndAllSubjectAndAllTeacherByStuid(id);
        return R.success(vo);
    }
}
